package com.xiaobai.frame.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaobai.common.utils.SnowFlow;
import com.xiaobai.common.utils.config.Tree.TreeHelper;
import com.xiaobai.common.utils.result.R;
import com.xiaobai.common.utils.result.Result;
import com.xiaobai.common.utils.result.ResultCode;
import com.xiaobai.common.utils.tool.AuthUtil;
import com.xiaobai.common.utils.tool.Func;
import com.xiaobai.data.manager.system.DeptManager;
import com.xiaobai.frame.service.IDeptService;
import com.xiaobai.model.convertor.system.DeptConvertor;
import com.xiaobai.model.entity.system.Dept;
import com.xiaobai.model.vo.system.dept.DeptTreeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static com.xiaobai.common.utils.tool.CommonConstants.ROOT_ID;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-24
 */
@Service
@AllArgsConstructor
public class DeptServiceImpl  implements IDeptService {

    private final DeptManager deptManager;

    @Override
    public Result addDept(Dept dept) {
        if (Func.isEmpty(dept)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
        Dept one = deptManager.getOne(new LambdaQueryWrapper<Dept>().eq(Dept::getId, dept.getParentId()), false);
        if (Func.isNotEmpty(one)) {
            dept.setAncestors(Func.isNull(one.getAncestors()) ? String.valueOf(one.getId()) : one.getAncestors() + "," + one.getId());
        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setCreateUser(AuthUtil.getId());
        dept.setId(new SnowFlow(1, 3).nextId());
        if (deptManager.save(dept)) {
            return R.success(ResultCode.SUCCESS_CREATE_RESOURCE, "新增成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "新增失败,请联系管理人员");
    }

    @Override
    public Result updateDept(Dept dept) {
        if (Func.isEmpty(dept) || Func.isNull(dept.getId())) {
            return R.fail(ResultCode.PARAMETER_NULL, "闯入参数为null");
        }
        if (deptManager.updateById(dept)) {
            return R.success(ResultCode.SUCCESS_NO_CONTENT, "修改成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "修改失败,请联系管理人员");
    }

    @Override
    @Transactional
    public Result deleteDept(Long[] ids) {
        if (Func.isEmpty(ids)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
        if (deptManager.removeBatchByIds(Arrays.asList(ids))) {
            return R.success(ResultCode.SUCCESS_CREATE_RESOURCE, "删除成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "删除失败,数据当中未有该对应数据");
    }

    @Override
    public Result listTreeDept(Long parentId) {
        List<Dept> deptList = deptManager.list();
        List<DeptTreeVO> treeVOList = DeptConvertor.INSTANCE.toTreeVOList(deptList);
        TreeHelper<Long, DeptTreeVO> helper = new TreeHelper<>(treeVOList, ROOT_ID, parentId);
        return R.success(helper.create());
    }

    @Override
    public Result getDept(Long id) {
        if (Func.isNull(id)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入数据未空");
        }
        Dept dept = deptManager.getById(id);
        if (Func.isEmpty(dept)) {
            return R.fail(ResultCode.NOT_SEEK_RESOURCE, "数据当中未有该id数据");
        }
        return R.success(dept);
    }
}
